home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / pd mix ii / access / thai / quizprint.c < prev    next >
C/C++ Source or Header  |  1994-05-20  |  8KB  |  398 lines

  1.  
  2. #include <stdio.h>
  3. #include "quiz.h"
  4.  
  5.  
  6. #define THAI_HEIGHT        22
  7. #define WIDTH            640
  8. #define HEIGHT            THAI_HEIGHT
  9. #define DEPTH            1
  10.  
  11. #define REV                2
  12.  
  13.  
  14. extern struct Screen *        OpenScreen ();
  15. extern struct Library *        OpenLibrary ();
  16.  
  17. extern LONG                    OpenPrinter ();
  18. extern void                    ClosePrinter ();
  19. extern LONG                    PrintString ();
  20. extern LONG                    DumpScreen ();
  21.  
  22.  
  23. struct IntuitionBase *        IntuitionBase;
  24. struct GfxBase *            GfxBase;
  25.  
  26. struct thai_phrase *        chosen_sentence;
  27. struct thai_phrase *        chosen_word;
  28. int                            file_changed = FALSE;
  29. struct thai_phrase            sentence_head;
  30. struct thai_phrase            word_head;
  31.  
  32. char scan_buf[ 3 ][ MAX_STRING ];
  33. struct thai_phrase scan = {
  34.     &scan_buf[0][0] , &scan_buf[1][0] , &scan_buf[2][0] , 0 , 0
  35. };
  36.  
  37.  
  38. struct NewScreen ns = {
  39.     0 , 200 - HEIGHT , WIDTH , HEIGHT , DEPTH ,
  40.     1 , 0 ,
  41.     HIRES ,
  42.     CUSTOMSCREEN ,
  43.     NULL ,
  44.     NULL ,
  45.     NULL ,
  46.     NULL
  47. };
  48.  
  49. struct Screen *screen = NULL;
  50.  
  51.  
  52. static char buf[ 16 ];
  53.  
  54.  
  55. struct IntuiText intui_text = {
  56.     1 , 0 , JAM1 , 0 , 0 ,
  57.     &thai_textattr , NULL , NULL
  58. };
  59.  
  60.  
  61. struct IntuiText intui_num = {
  62.     1 , 0 , JAM1 , 0 , 0 ,
  63.     NULL , NULL , NULL
  64. };
  65.  
  66.  
  67. int num_sentences , num_words;
  68. int print_thai , print_english , print_phonetic;
  69.  
  70.  
  71. main ( argc , argv )
  72. int argc;
  73. char **argv;
  74. {
  75.     struct thai_phrase *p , *chosen , *list;
  76.     int word_opt , sent_opt;
  77.  
  78.  
  79.     print_thai = FALSE;
  80.     print_english = FALSE;
  81.     print_phonetic = FALSE;
  82.     word_opt = FALSE;
  83.     sent_opt = FALSE;
  84.     num_sentences = 15;
  85.     num_words = 15 * 2;
  86.  
  87.     while ( argc > 1  &&  argv[1][0] == '-' ) {
  88.         switch ( argv[1][1] ) {
  89.  
  90.         case 't' :
  91.             print_thai = TRUE;
  92.             break;
  93.  
  94.         case 'p' :
  95.             print_phonetic = TRUE;
  96.             break;
  97.  
  98.         case 'e' :
  99.             print_english = TRUE;
  100.             break;
  101.  
  102.         case 'w' :
  103.             word_opt = TRUE;
  104.             if ( argv[1][2] != '\0' ) {
  105.                 if ( sscanf ( argv[1]+2 , "%d" , &num_words ) != 1 )
  106.                     usage ();
  107.             }
  108.             break;
  109.  
  110.         case 's' :
  111.             sent_opt = TRUE;
  112.             if ( argv[1][2] != '\0' ) {
  113.                 if ( sscanf ( argv[1]+2 , "%d" , &num_sentences ) != 1 )
  114.                     usage ();
  115.             }
  116.             break;
  117.  
  118.         default :
  119.             usage ();
  120.  
  121.         }
  122.         argc--;
  123.         argv++;
  124.     }
  125.  
  126.     if ( word_opt == FALSE  &&  sent_opt == FALSE ) {
  127.         word_opt = TRUE;
  128.         sent_opt = TRUE;
  129.     }
  130.  
  131.     if ( ( !print_thai )  &&  ( !print_english )  &&  ( !print_phonetic ) ) {
  132.         print_thai = TRUE;
  133.         print_english = TRUE;
  134.         print_phonetic = TRUE;
  135.     }
  136.  
  137.     if ( argc > 1 )
  138.         usage ();
  139.  
  140.     if ( ( IntuitionBase = (struct IntuitionBase *)
  141.         OpenLibrary ( "intuition.library" , (LONG)REV ) ) == NULL ) {
  142.  
  143.         fprintf ( stderr , "Failed to open intuition.library\n" );
  144.     }
  145.     else {
  146.         if ( ( GfxBase = (struct GfxBase *)
  147.             OpenLibrary ( "graphics.library" , (LONG)REV ) ) == NULL ) {
  148.  
  149.             fprintf ( stderr , "Failed to open graphics.library\n" );
  150.         }
  151.         else {
  152.             if ( !open_screen () ) {
  153.                 fprintf ( stderr , "Failed to open screen\n" );
  154.             }
  155.             else {
  156.                 if ( !open_thai_font () ) {
  157.                     fprintf ( stderr , "Failed to open thai font\n" );
  158.                 }
  159.                 else {
  160.                     if ( ! OpenPrinter () ) {
  161.                         fprintf ( stderr , "Failed to open printer\n" );
  162.                     }
  163.                     else {
  164.                         load_phrases ();
  165.  
  166.                         if ( sent_opt )
  167.                             print_sentences ();
  168.                         if ( word_opt )
  169.                             print_words ();
  170.  
  171.                         /*unload_phrases (); not needed at present  - never change words */
  172.  
  173.                         ClosePrinter ();
  174.                     }
  175.                     close_thai_font ();
  176.                 }
  177.                 close_screen ();
  178.             }
  179.             CloseLibrary ( GfxBase );
  180.         }
  181.         CloseLibrary ( IntuitionBase );
  182.     }
  183. }
  184.  
  185.  
  186. usage ()
  187. {
  188.     fprintf ( stderr , "usage: quizprint [-t] [-p] [-e] [-sN] [-wN]\n" );
  189.     exit ( 1 );
  190. }
  191.  
  192.  
  193. open_screen ()
  194. {
  195.     screen = OpenScreen ( &ns );
  196.     if ( screen != NULL ) {
  197.         SetRGB4 ( &screen->ViewPort , (LONG)1 , (LONG)0 , (LONG)0 , (LONG)0 );
  198.         SetRGB4 ( &screen->ViewPort , (LONG)0 , (LONG)15, (LONG)15, (LONG)15 );
  199.         SetRast ( &screen->RastPort , (LONG)0 );
  200.         return ( TRUE );
  201.     }
  202.     return ( FALSE );
  203. }
  204.  
  205.  
  206. close_screen ()
  207. {
  208.     if ( screen != NULL ) {
  209.         CloseScreen ( screen );
  210.         screen = NULL;
  211.     }
  212. }
  213.  
  214.  
  215. print_sentences ()
  216. {
  217.     struct thai_phrase *list , *p;
  218.     int i;
  219.     int line;
  220.  
  221.  
  222.     list = NULL;
  223.     while ( num_sentences-- > 0  &&  sentence_head.next != NULL ) {
  224.         random_sentence ();
  225.         if ( chosen_sentence != NULL ) {
  226.  
  227.             /* unlink from list */
  228.  
  229.             for ( p = &sentence_head;
  230.             p->next != chosen_sentence;
  231.             p = p->next )
  232.                 ;
  233.  
  234.             p->next = chosen_sentence->next;
  235.  
  236.             /* add to list of sentences to print */
  237.  
  238.             chosen_sentence->next = list;
  239.             list = chosen_sentence;
  240.         }
  241.     }
  242.  
  243.     /* now print list of selected sentences in english */
  244.  
  245.     line = 0;
  246.     if ( print_english ) {
  247.         i = 1;
  248.         for ( p = list; p != NULL; p = p->next ) {
  249.             sprintf ( buf , "(%d) " , i++ );
  250.             if ( PrintString ( buf ) != 0 ) return;
  251.             if ( PrintString ( p->english ) != 0 ) return;
  252.             if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
  253.             line += 4;
  254.         }
  255.         while ( ( line++ % 66 ) != 0 )
  256.             if ( PrintString ( "\n" ) != 0 ) return;
  257.     }
  258.  
  259.     /* print in phonetical form */
  260.  
  261.     line = 0;
  262.     if ( print_phonetic ) {
  263.         i = 1;
  264.         for ( p = list; p != NULL; p = p->next ) {
  265.             sprintf ( buf , "(%d) " , i++ );
  266.             if ( PrintString ( buf ) != 0 ) return;
  267.             if ( PrintString ( p->phonetic ) != 0 ) return;
  268.             if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
  269.             line += 4;
  270.         }
  271.         while ( ( line++ % 66 ) != 0 )
  272.             if ( PrintString ( "\n" ) != 0 ) return;
  273.     }
  274.  
  275.     /* print in thai form */
  276.  
  277.     line = 0;
  278.     if ( print_thai ) {
  279.         i = 1;
  280.         for ( p = list; p != NULL; p = p->next ) {
  281.             sprintf ( buf , "(%d)" , i );
  282.             intui_num.IText = (UBYTE *) buf;
  283.             PrintIText ( &screen->RastPort , &intui_num ,
  284.                 (LONG)0 , (LONG)10 );
  285.             intui_text.IText = (UBYTE *) p->thai;
  286.             PrintIText ( &screen->RastPort , &intui_text ,
  287.                 (LONG)5*8 , (LONG)0 );
  288.             if ( DumpScreen ( screen ) != 0 ) return;
  289.             if ( PrintString ( "\n\n" ) != 0 ) return;
  290.             SetRast ( &screen->RastPort , (LONG)0 );
  291.             i++;
  292.             line += 4;
  293.         }
  294.         while ( ( line++ % 66 ) != 0 )
  295.             if ( PrintString ( "\n" ) != 0 ) return;
  296.     }
  297. }
  298.  
  299.  
  300. print_words ()
  301. {
  302.     struct thai_phrase *list , *p;
  303.     int i , x , line;
  304.  
  305.  
  306.     list = NULL;
  307.     while ( num_words-- > 0  &&  word_head.next != NULL ) {
  308.         random_word ();
  309.         if ( chosen_word != NULL ) {
  310.  
  311.             /* unlink from list */
  312.  
  313.             for ( p = &word_head;
  314.             p->next != chosen_word;
  315.             p = p->next )
  316.                 ;
  317.  
  318.             p->next = chosen_word->next;
  319.  
  320.             /* add to list of words to print */
  321.  
  322.             chosen_word->next = list;
  323.             list = chosen_word;
  324.         }
  325.     }
  326.  
  327.     /* now print list of selected words in english */
  328.  
  329.     line = 0;
  330.     if ( print_english ) {
  331.         i = 1;
  332.         for ( p = list; p != NULL; p = p->next ) {
  333.             sprintf ( buf , "(%d) " , i++ );
  334.             if ( PrintString ( buf ) != 0 ) return;
  335.             if ( PrintString ( p->english ) != 0 ) return;
  336.             if ( PrintString ( "\n\n" ) != 0 ) return;
  337.             line += 2;
  338.         }
  339.         while ( ( line++ % 66 ) != 0 )
  340.             if ( PrintString ( "\n" ) != 0 ) return;
  341.     }
  342.  
  343.     /* print in phonetical form */
  344.  
  345.     line = 0;
  346.     if ( print_phonetic ) {
  347.         i = 1;
  348.         for ( p = list; p != NULL; p = p->next ) {
  349.             sprintf ( buf , "(%d) " , i++ );
  350.             if ( PrintString ( buf ) != 0 ) return;
  351.             if ( PrintString ( p->phonetic ) != 0 ) return;
  352.             if ( PrintString ( "\n\n" ) != 0 ) return;
  353.             line += 2;
  354.         }
  355.         while ( ( line++ % 66 ) != 0 )
  356.             if ( PrintString ( "\n" ) != 0 ) return;
  357.     }
  358.  
  359.     /* print in thai form */
  360.  
  361.     line = 0;
  362.     if ( print_thai ) {
  363.         i = 1;
  364.         for ( p = list; p != NULL; p = p->next ) {
  365.  
  366.             switch ( i % 4 ) {
  367.             case 1 : x = 0; break;
  368.             case 2 : x = 160; break;
  369.             case 3 : x = 320; break;
  370.             case 0 : x = 480; break;
  371.             }
  372.  
  373.             sprintf ( buf , "(%d)" , i );
  374.             intui_num.IText = (UBYTE *) buf;
  375.             PrintIText ( &screen->RastPort , &intui_num ,
  376.                 (LONG)x , (LONG)10 );
  377.             intui_text.IText = (UBYTE *) p->thai;
  378.             PrintIText ( &screen->RastPort , &intui_text ,
  379.                 (LONG)x + 5*8 , (LONG)0 );
  380.             if ( ( i % 4 ) == 0 ) {
  381.                 if ( DumpScreen ( screen ) != 0 ) return;
  382.                 if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
  383.                 SetRast ( &screen->RastPort , (LONG)0 );
  384.                 line += 6;
  385.             }
  386.             i++;
  387.         }
  388.         if ( ( i % 4 ) != 1 ) {
  389.             if ( DumpScreen ( screen ) != 0 ) return;
  390.             if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
  391.             SetRast ( &screen->RastPort , (LONG)0 );
  392.             line += 6;
  393.         }
  394.         while ( ( line++ % 66 ) != 0 )
  395.             if ( PrintString ( "\n" ) != 0 ) return;
  396.     }
  397. }
  398.